﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Administration;
using eAssessment.Classes.Assessments;
using Microsoft.SharePoint.Utilities;
using System.Web;
using System.Collections.Generic;
using eAssessment.lists;

namespace eAssessment.Webparts.AddQuestionToAssessment
{
    public partial class AddQuestionToAssessmentUserControl : UserControl
    {
        private CreateAssessmentInput _input;

        protected void Page_Load(object sender, EventArgs e)
        {
             _input = (CreateAssessmentInput)Session["CreateQuestionInput"];
            if (!Page.IsPostBack)
            {
                ListAccessManager mgr = new ListAccessManager();
                DataTable dt = mgr.GetQuestionTemplateDataTable();

                RemoveAlreadyAddedQuestions(dt);

                Session["addQuestionTable"] = dt;

                questionGridView.Columns.Add(new SPBoundField { DataField = "ID", HeaderText = "ID", SortExpression = "ID" });
                questionGridView.Columns.Add(new SPBoundField { DataField = "Theme", HeaderText = "Thema", SortExpression = "Theme" });
                questionGridView.Columns.Add(new SPBoundField { DataField = "Question", HeaderText = "Frage", SortExpression = "Question" });
                questionGridView.Columns.Add(new SPBoundField { DataField = "Type", HeaderText = "Typ", SortExpression = "Type" });

                questionGridView.PageSize = dt.Rows.Count;

                questionGridView.DataSource = Session["addQuestionTable"];
                questionGridView.DataBind();
            }

           
        }

        protected void questionGridView_Sorting(object sender, GridViewSortEventArgs e)
        {
            DataTable dt = Session["addQuestionTable"] as DataTable;

            if (dt != null)
            {
                dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
                questionGridView.DataSource = Session["addQuestionTable"];
                questionGridView.DataBind();
            }
        }

        private string GetSortDirection(string column)
        {
            string sortDirection = "ASC";
            string sortExpression = ViewState["SortExpression"] as string;

            if (sortExpression != null)
            {
                if (sortExpression == column)
                {
                    string lastDirection = ViewState["SortDirection"] as string;
                    if ((lastDirection != null) && (lastDirection == "ASC"))
                    {
                        sortDirection = "DESC";
                    }
                }
            }
            ViewState["SortDirection"] = sortDirection;
            ViewState["SortExpression"] = column;

            return sortDirection;
        }

        protected void addQuestionsButton_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in questionGridView.Rows)
            {
                ControlCollection controls = row.Cells[0].Controls;
                CheckBox box = (CheckBox)controls[1];
                if (box.Checked)
                {
                    Label l = (Label)row.Cells[1].Controls[0];
                    _input.AddQuestion(int.Parse(l.Text));
                }
            }
            Session["CreateQuestionInput"] = _input;
            SPUtility.Redirect("CreateAssessment.aspx", SPRedirectFlags.Default, HttpContext.Current);
        }

        private void RemoveAlreadyAddedQuestions(DataTable dt)
        {
            List<DataRow> rowsToRemove = new List<DataRow>();

            foreach (DataRow row in dt.Rows)
            {
                foreach (int id in _input.Questions)
                {
                    if (id == (int)row.ItemArray[0])
                    {
                        rowsToRemove.Add(row);
                        break;
                    }
                }
            }
            foreach (DataRow row in rowsToRemove)
            {
                dt.Rows.Remove(row);
            }
        }

    }
}
